package com.example.leetdemo.finash;

import java.util.ArrayList;
import java.util.List;

public class Solution56 {


    public int[][] merge(int[][] intervals) {
        // 冒泡排序 T-T 别的不想写
        for (int i = 0; i < intervals.length; i++) {
            for (int j = 1; j < intervals.length - i; j++) {
                if (intervals[j][0] < intervals[j - 1][0]) {
                    int[] temp = intervals[j - 1];
                    intervals[j - 1] = intervals[j];
                    intervals[j] = temp;
                }
            }
        }
        List<int[]> arr = new ArrayList<>();
        int[] temp = null;
        for (int i = 0; i < intervals.length; i++) {
            if (temp == null) {
                temp = intervals[i];
                continue;
            }
            if (temp[1] >= intervals[i][0]) {
                temp[1] = Math.max(intervals[i][1], temp[1]);
            } else {
                arr.add(temp);
                temp = intervals[i];
            }
        }
        if (temp != null) {
            arr.add(temp);
        }
        int[][] re = new int[arr.size()][2];
        for (int i = 0; i < re.length; i++) {
            re[i] = arr.get(i);
        }
        return re;
    }

    public static void main(String[] args) {
        Solution56 solution56 = new Solution56();
        solution56.merge(new int[][] {
                {1,3},{2,6},{8,10},{15,18}
        });
    }

}
